package com.ruoyi.system.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.service.ISysAssetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.SysAssetCategory;
import com.ruoyi.system.service.ISysAssetCategoryService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 资产分类Controller
 * 
 * @author sxy
 * @date 2025-03-25
 */
@RestController
@RequestMapping("/category")
public class SysAssetCategoryController extends BaseController
{
    @Autowired
    private ISysAssetCategoryService categoryService;
    @Autowired
    private ISysAssetService sysAssetService;
    /**
     * 获取分类树
     */
    @RequiresPermissions("system:category:list")
    @GetMapping("/tree")
    public AjaxResult categoryTree(SysAssetCategory sysAssetCategory){
        return AjaxResult.success(categoryService.selectCategoryTreeList(sysAssetCategory));
    }

    /**
     * 查询资产分类列表
     */
    @RequiresPermissions("system:category:list")
    @GetMapping("/list")
    public AjaxResult list(SysAssetCategory sysAssetCategory)
    {
        List<SysAssetCategory> list = categoryService.selectSysAssetCategoryList(sysAssetCategory);
        return AjaxResult.success(list);
    }

    /**
     * 查询资产分类列表(排除自己)
     */
    @RequiresPermissions("system:category:list")
    @GetMapping("/list/exclude/{categoryId}")
    public AjaxResult list(@PathVariable("categoryId") Long categoryId)
    {
        List<SysAssetCategory> list = categoryService.selectSysAssetCategoryList(new SysAssetCategory());
        list.removeIf(item -> item.getCategoryId().intValue() == categoryId);
        return AjaxResult.success(list);
    }

    /**
     * 导出资产分类列表
     */
    @RequiresPermissions("system:category:export")
    @Log(title = "资产分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysAssetCategory sysAssetCategory)
    {
        List<SysAssetCategory> list = categoryService.selectSysAssetCategoryList(sysAssetCategory);
        ExcelUtil<SysAssetCategory> util = new ExcelUtil<SysAssetCategory>(SysAssetCategory.class);
        util.exportExcel(response, list, "资产分类数据");
    }

    /**
     * 获取资产分类详细信息
     */
    @RequiresPermissions("system:category:query")
    @GetMapping(value = "/{categoryId}")
    public AjaxResult getInfo(@PathVariable("categoryId") Long categoryId)
    {
        return success(categoryService.selectSysAssetCategoryByCategoryId(categoryId));
    }

    /**
     * 新增资产分类
     */
    @RequiresPermissions("system:category:add")
    @Log(title = "资产分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysAssetCategory sysAssetCategory)
    {
        return toAjax(categoryService.insertSysAssetCategory(sysAssetCategory));
    }

    /**
     * 修改资产分类
     */
    @RequiresPermissions("system:category:edit")
    @Log(title = "资产分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysAssetCategory sysAssetCategory)
    {
        //先判断分类名称是否重复
        if(categoryService.checkCategoryNameUnique(sysAssetCategory.getCategoryName()) > 0){
            return AjaxResult.warn("分类名称重复！");
        }
        //判断父级分类id是否为自己
        if(sysAssetCategory.getCategoryId() == sysAssetCategory.getParentId()){
            return AjaxResult.warn("父级分类不能是自己！");
        }
        //判断是否有子分类
        if(categoryService.selectListByParentId(sysAssetCategory.getCategoryId()) > 0){
            return AjaxResult.warn("该分类下有子分类！");
        }
        return toAjax(categoryService.updateSysAssetCategory(sysAssetCategory));
    }

    /**
     * 删除资产分类
     */
    @RequiresPermissions("system:category:remove")
    @Log(title = "资产分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{categoryIds}")
    public AjaxResult remove(@PathVariable Long categoryId)
    {
        //先判断该分类下是否有子分类
        if(categoryService.selectListByParentId(categoryId) > 0){
            return AjaxResult.warn("该分类下有子分类，不能删除");
        }
        //再判断当前节点下是否有资产
        if(sysAssetService.countByCategoryId(categoryId) > 0){
            return AjaxResult.warn("该分类下有资产，不能删除");
        }
        return toAjax(categoryService.deleteSysAssetCategoryByCategoryId(categoryId));
    }
}
